//
//  LYEmoticonCell.swift
//  EmoticonKeyboard
//
//  Created by liuerying on 16/5/30.
//  Copyright © 2016年 liuerying. All rights reserved.


//21个按钮视图  一个cell 对应一个数组

import UIKit
import SnapKit

private let margin:CGFloat = 5
let EmotionPageCount = 21
let EmotionColCount = 7
let EmotionRowCount = 3
private let BottomMargin:CGFloat = 30

class LYEmoticonCell: UICollectionViewCell {
 
    var emoticons : [LYEmoticon]?{
    
        didSet{
        
            for (index,emoticon) in emoticons!.enumerate(){
            
                
                let btn = buttons[index]
                
                btn.emoticon = emoticon
              
            }
        }
    }
    
    
    var indexPath:NSIndexPath?{
    
        didSet{
        
            textLabel.text = "\(indexPath?.section ?? 0)行,\(indexPath?.item ?? 0)个"
            
        }
    }
    
    //定义按钮数组
    private lazy var buttons: [LYEmoticonButton] = [LYEmoticonButton]()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.contentView.backgroundColor = UIColor(white: 0.9, alpha: 1)
        setupUI()
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置UI界面
    private func setupUI(){
        addSubview(textLabel)
        
        
        textLabel.snp_makeConstraints { (make) in
            
            make.center.equalTo(contentView.snp_center)
        }

        for i in 0..<21{
            let width = (UIScreen.mainScreen().bounds.width - 2 * margin) / CGFloat( EmotionColCount)
        
            let height = (self.bounds.height - BottomMargin) / CGFloat(EmotionRowCount)
            
            let btn = LYEmoticonButton()
        
            let row = i / EmotionColCount
            let col = i % EmotionColCount
            
            let x:CGFloat = margin + width * CGFloat(col)
            let y:CGFloat = CGFloat(row) * height
            
            btn.frame = CGRect(x: x, y: y, width: width, height: height)
            
            //设置测试文字
//            btn.setTitle("嘻哈", forState: .Normal)
           
            //设置图片的大小
            btn.titleLabel?.font = UIFont.systemFontOfSize(32)
            
            btn.addTarget(self, action: #selector(LYEmoticonCell.btnDidClick(_:)), forControlEvents: .TouchUpInside)
//            btn.backgroundColor = randomColor()
            
            self.contentView.addSubview(btn)
            buttons.append(btn)
            
        }
        
        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(LYEmoticonCell.longPressFire(_:)))
        
        self.contentView.addGestureRecognizer(longPress)
    
    }
    
    
    
    @objc private func btnDidClick(btn:LYEmoticonButton){
    
        NSNotificationCenter.defaultCenter().postNotificationName(EmoticonBtnDidClickNotification, object: btn.emoticon)
    }
    
    
    
    @objc private func longPressFire(ges:UILongPressGestureRecognizer){
    
        let point = ges.locationInView(contentView)
        
        guard let btn = buttonWithPoint(point) else{
        
            popView.dismissAfter()
            
            return
        }
        
        if btn.emoticon!.isDelete || btn.emoticon!.isEmpty{
        
            popView.dismissAfter()
            
            return
            
        }
        
        switch ges.state {
        
        case .Began,.Changed:
          
            popView.show(btn)
            
        default:
            popView.dismissAfter()
        }
        
    }
    //MARK: 根据手势触摸点 获取对应的按钮
    private func buttonWithPoint(point:CGPoint) -> LYEmoticonButton?{
    
        for btn in buttons {
        
            if btn.frame.contains(point){
            
                return btn
            }
            
        }
        
        return nil
        
    }
    
    //懒加载控件
    private lazy var popView: LYEmoticonPopView = {
    
        let pop = LYEmoticonPopView.loadPopView()
        return pop
    }()
    
    private lazy var textLabel: UILabel = {
    
        let l = UILabel()
        l.font = UIFont.systemFontOfSize(18)
//        l.textColor = UIColor.whiteColor()

        l.sizeToFit()
        return l
    }()
    
}
